<?php

namespace Biz\model\theParagraph;


class Room extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_the_paragraph';

    const _DATABASE_ = 'the_paragraph';

    const _TABLE_NAME = 'room';

    // 状态为正常
    const STATUS_NORMAL = 1;
    // 状态为隐藏
    const STATUS_HIDDEN = 2;
    // 状态为被删除
    const STATUS_DELETE = 9;
    const DEFAULE_DATETIME = '0000-00-00 00:00:00';

    const ROOM_TYPE_DEFAULE = 1;
    const ROOM_TYPE_PAGE = 2;

    const SORT_HOMEPAGE = 'sort';
    const SORT_INDEX = 'index_sort';

    public static function getRoomListByStoreId($storeId, $status = [self::STATUS_NORMAL], $sort = self::SORT_HOMEPAGE, $type = [
        self::ROOM_TYPE_DEFAULE,
        self::ROOM_TYPE_PAGE
    ])
    {
        return self::select("*", [
            'store_id' => $storeId,
            'status' => $status,
            'type' => $type,
            'ORDER' => [
                $sort,
                'id'
            ]
        ]);
    }

    public static function getRoomsByIds($ids)
    {
        return self::select("*", [
            'id' => $ids,
            'status' => self::STATUS_NORMAL
        ]);
    }

    public static function getRoomById($id)
    {
        return self::get("*", ['id' => $id]);
    }

    public static function saveRoomInfo($roomInfo, $id = 0)
    {
        if ($id) {
            $pageInfo['edittime'] = date("Y-m-d H:i:s");
            if (self::update($roomInfo, ["id" => $id])->rowcount()) {
                return $id;
            } else {
                return false;
            }
        } else {
            $roomInfo['status'] = self::STATUS_HIDDEN;
            $roomInfo['addtime'] = date("Y-m-d H:i:s");
            if (self::insert($roomInfo)->rowcount()) {
                return self::id();
            } else {
                return false;
            }
        }
    }

    public static function changeRoomStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    public static function saveRoomSort($sortInfo, $sortKey = self::SORT_HOMEPAGE)
    {
        foreach ($sortInfo as $RoomId => $sort) {
            self::update([$sortKey => $sort], ['id' => $RoomId]);
        }
    }

    public static function getBasicInfoByAdmin($id, $field = 'title')
    {
        return self::get($field, ['id' => $id, 'status' => self::STATUS_NORMAL]);
    }

}

